package com.itlong.cloud.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.itlong.cloud.POJO.PO.PropertyParkPassRecordPO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.dao.IParkingPassRecordDao;
import com.itlong.cloud.enumerate.FileCatalogEnum;
import com.itlong.cloud.service.IParkingPassRecordService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.file.UploadFileUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <desc>
 *      物业-智慧停车-停车场出入记录接口实现类.
 * </desc>
 *
 * @createDate 2018/11/5
 */
@Service("iParkingPassRecordService")
@RefreshScope
public class ParkingPassRecordServiceImpl implements IParkingPassRecordService {


    @Value("${" + PlatformConstants.FILE_FTP_HOST + "}")
    public String FILE_FTP_HOST;

    @Value("${" + PlatformConstants.FILE_FTP_PORT + "}")
    public String FILE_FTP_PORT;

    @Value("${" + PlatformConstants.FILE_FTP_USERNAME + "}")
    public String FILE_FTP_USERNAME;

    @Value("${" + PlatformConstants.FILE_FTP_PASSWORD + "}")
    public String FILE_FTP_PASSWORD;

    @Value("${" + PlatformConstants.FILE_ACCESS_URL + "}")
    public String FILE_ACCESS_URL;

    @Autowired
    IParkingPassRecordDao iParkingPassRecordDao;

    /**
     * <desc>
     *      保存车辆进场记录
     * </desc>
     *
     * @param json 对象json字符串
     * @return
     * @author Jiaqi.X
     * @createDate 2018/11/08
     */
    @Override
    public Integer addInPassRecord(String key,String json) throws Exception {
        List<PropertyParkPassRecordPO> records = JsonUtil.decode(json,
                new TypeReference<List<PropertyParkPassRecordPO>>() {});
        int result = 0 ;
        String projectId = iParkingPassRecordDao.getProjectIdByKey(key);
        for(PropertyParkPassRecordPO record : records){
            //上传图片
            InputStream input = new ByteArrayInputStream(StringHandlerUtil.imageBase64StrToByte(record.getInPicFirst()));
            record.setInPicFirst(this.uploadFile(input, "png"));
            record.setParkPassId(LogicIdUtil.bussinessId());
            record.setStatus(1);
            record.setAppStatus(1);
            record.setCreateTime(new Date());
            record.setProjectId(projectId);
            //查询是否是月卡车
            record.setCarType(iParkingPassRecordDao.getCarMonthCardInfoByPlateNumber(projectId,record.getPlateNumber()) > 0 ? 1 : 2);
            result = iParkingPassRecordDao.deleteReportCar(projectId,record.getPlateNumber());
            result = iParkingPassRecordDao.save(SqlUtil.durableData(record, PlatformConstants.TABLE_SAVE)) > 0 ? result+1:result;
        }
        return result;
    }

    /**
     * <desc>
     *      保存车辆出场记录
     * </desc>
     *
     * @param json 对象json字符串
     * @return
     * @author Jiaqi.X
     * @createDate 2018/11/08
     */
    @Override
    public Integer addOutPassRecord(String key,String json) throws Exception {
        List<PropertyParkPassRecordPO> records = JsonUtil.decode(json,
                new TypeReference<List<PropertyParkPassRecordPO>>() {});
        int result = 0 ;
        for(PropertyParkPassRecordPO record : records){
            //上传图片
            InputStream input = new ByteArrayInputStream(StringHandlerUtil.imageBase64StrToByte(record.getOutPicFirst()));
            record.setOutPicFirst(this.uploadFile(input, "png"));
            record.setParkPassId(iParkingPassRecordDao.getParkPassIdByParkingIdAndParkId(record.getParkingId(),record.getParkId()));
            record.setUpdateTime(new Date());
            result = iParkingPassRecordDao.update(SqlUtil.durableData(record, PlatformConstants.TABLE_UPDATE)) > 0 ? result+1:result;
        }
        return result;
    }

    /**
     * <desc>
     *      Ftp上传文件，返回Url.
     * </desc>
     *
     * @param inputStream 文件流
     * @param fileType 文件类型
     * @return 文件上传成功后会返回Url,否则返回空
     * @author Qiang.S
     * @createDate 2019/02/22
     */
    private String uploadFile(InputStream inputStream, String fileType) throws IOException {
        String fileUrl = "";
        UploadFileUtil.UploadFile uploadFileUtil = new UploadFileUtil().new UploadFile(
                FILE_FTP_HOST,
                FILE_FTP_USERNAME,
                FILE_FTP_PASSWORD,
                FILE_FTP_PORT,
                inputStream,
                fileType,
                FILE_ACCESS_URL,
                FileCatalogEnum.PARK_IMG_FILE.getType()
        );
        Map<String, Object> resultMap = UploadFileUtil.uploadFile(uploadFileUtil);
        Boolean returnCode = (Boolean)resultMap.get("returnCode");
        if (returnCode == false){
            return fileUrl;
        }
        fileUrl = resultMap.get("accessPath").toString();
        return fileUrl;
    }
}